package pervasivePositioning.pos;
import android.location.Location;

public class MultiStrategy extends ReportingStrategy {
	
	Location lastFix = null;
	float lastAcceleration[] = {0,0,0};
	boolean isInMotion = false;
	
	boolean isPeriodicStrategy = true;
	boolean isDistanceStrategy = false;
	boolean isSpeedStrategy = false;
	boolean isAccelerationStrategy = false;
	
	long minDelay = 1000;
	float minDist;
	float maxSpeed;
	float accThreshold;

	public MultiStrategy() {
	}
	
	public void setDelay(long msec) {
		if (msec >= 0)
			minDelay = msec;
	}

	public void setDist(float m) {
		if (m > 0)
			minDist = m;
	}
	
	public void setSpeed(float ms) {
		if (ms > 0)
			maxSpeed = ms;
	}
	
	public void setAccTreshold(float t) {
		if (t > 0)
			accThreshold = t;		
	}
	
	@Override
	public boolean reportPosition(Location loc) {
		long time = loc.getTime();
		float dist = loc.distanceTo(lastFix);
		long deltaTime = loc.getTime() - lastFix.getTime();
		float speed = (deltaTime > 0)? dist/deltaTime : 0;

		if (isPeriodicStrategy && time < lastFix.getTime() + minDelay) {
			return false;
		}
		
		if (isDistanceStrategy && dist < minDist) {
			return false;
		}

		if (isSpeedStrategy && speed > maxSpeed) {
			return false;			
		}  

		if (isAccelerationStrategy && isInMotion) {
			return false;			
		}  

		
		lastFix = loc;
		return true;
	}

	@Override
	public String getName() {
		return "MultiStrategy";
	}

	@Override
	public String getConfig() {
		String config = "";
		if (isPeriodicStrategy) {
			config += String.format("%s=%f, ", "minDelay", (float) minDelay/1000.);
		}
		if (isDistanceStrategy) {
			config += String.format("%s=%f, ", "minDist", minDist);
		}
		if (isSpeedStrategy) {
			config += String.format("%s=%f, ", "maxSpeed", maxSpeed);
		}
		if (isAccelerationStrategy) {
			config += "motion=yes, ";
		}
/*		if (config.endsWith(", ")) {
 			fjern
		}*/
		
		return config;
	}

	public boolean reportAcceleration(float[] values) {
		isInMotion = (Math.abs(lastAcceleration[0] - values[0]) > accThreshold
				|| Math.abs(lastAcceleration[1] - values[1]) > accThreshold
				|| Math.abs(lastAcceleration[2] - values[2]) > accThreshold);
		if(isInMotion) {
			lastAcceleration = values;
		}
		
		return isAccelerationStrategy && isInMotion;
	}

	@Override
	void setValue(float v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	float getValue() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getValueName() {
		// TODO Auto-generated method stub
		return null;
	}


}


